package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    private static final Logger log = LoggerFactory.getLogger(ShoppingCartServiceImpl.class);
    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;


    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //设置id（threadLocal）
        log.info("此时用户id的值是：{}",BaseContext.getCurrentId());
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断当前商品是否在购物车中
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);



        if (shoppingCartList != null && shoppingCartList.size() == 1) {

            //如果已经存在，就更新数量，数量加1
            ShoppingCart cart= shoppingCartList.get(0);
            cart.setNumber(cart.getNumber() + 1);

            shoppingCartMapper.updateNumberById(cart);
        }else {//购物车里没有数据
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            if (shoppingCart.getDishId()!=null){//即添加的是菜品
                DishVO dishVO = dishMapper.selectById(shoppingCart.getDishId());
                shoppingCart.setName(dishVO.getName());
                shoppingCart.setAmount(dishVO.getPrice());
                shoppingCart.setImage(dishVO.getImage());
            }else {//即添加的是套餐
                SetmealVO setmealVO = setmealMapper.selectById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setAmount(setmealVO.getPrice());
                shoppingCart.setImage(setmealVO.getImage());


            }
            log.info("执行了加入购物车！!!");
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 显示购物车
     * @return
     */
    @Override
    public List<ShoppingCart> listAll() {
        Long currentId = BaseContext.getCurrentId();
        List<ShoppingCart>  res =  shoppingCartMapper.selectAll(currentId);
        return res;

    }

    /**
     * 清空购物车
     */
    @Override
    public void deleteAll() {
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteAll(currentId);

    }

    @Override
    public void deleteSingle(ShoppingCartDTO shoppingCartDTO) {
        if (shoppingCartDTO.getDishId() != null) {//那就是删除购物车的菜品
            shoppingCartMapper.deleteSingleDish(shoppingCartDTO);
        }else {//那就是删除购物车的套餐

            shoppingCartMapper.deleteSingleSetmeal(shoppingCartDTO);
        }
    }
}
